package com.funcity.stage.elements
{
	import com.funcity.cache.ResourceCacher;
	import com.funcity.events.CacheEvent;
	import com.funcity.stage.GameStage;
	import com.funcity.stage.layers.GroundLayer;
	
	import flash.debugger.enterDebugger;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.text.TextField;
	
	public final class GroundTile extends Bitmap
	{
		private var _dimension:uint = 0;
		private var _horizontalDimension:uint;
		private var _verticalDimension:uint;
		private var _width:uint = 0;
		private var _height:uint = 0;
		
		private var _mapURL:String;
		private var _curURL:String;
		private var _xOrigin:uint = 0;
		private var _yOrigin:uint = 0;

		
		private var _loaded:Boolean = false;
		private var _canGetPreCenter:Boolean = false;
		
		private var _centerPixInScene:Point = new Point;
		private var _centerGridGridInScene:Point = new Point;
		
		private var _gridInScene:Point = new Point;
		private var _pixInScene:Point = new Point;//depre
		private var _gridInView:Point = new Point;
		private var _pixInView:Point = new Point;//depre  x y
		
		public function GroundTile(horizontalDimension:uint, verticalDimension:uint, width:uint, height:uint, xCoord:uint, yCoord:uint)
		{
			super(null, "auto", false);
			
			//_dimension = dimension;
			_horizontalDimension = horizontalDimension;
			_verticalDimension = verticalDimension;
			_width = width;
			_height = height;
			
			_gridInView.x = xCoord;
			_gridInView.y = yCoord;
			
			x = _gridInView.x * _width;
			y = _gridInView.y * _height;
		}
		
		public function changeMap(mapURL:String, xcP:uint, ycP:uint):void
		{
			if (_mapURL)
			{
				var url:String = getURL();
				clean(url);
			}
			_mapURL = mapURL;
			
			_loaded = false;
			_canGetPreCenter = false;

			centerTo(xcP, ycP);
		}
		
		public function move(xDirect:int, yDirect:int):void
		{
			var url:String = getURL3();
			
			// move the origin of map
			_xOrigin += xDirect;
			_yOrigin += yDirect;
			
			// calculate new X coordinate 
			var xReload:Boolean = false;
			_gridInView.x -= xDirect;
			xReload = (_gridInView.x < 0 || _gridInView.x >= _horizontalDimension);			
			_gridInView.x = _gridInView.x >= 0 ? _gridInView.x % _horizontalDimension : _gridInView.x % _horizontalDimension + _horizontalDimension;

			// calculate new Y coordinate 
			var yReload:Boolean = false;
			_gridInView.y -= yDirect;
			yReload = (_gridInView.y < 0 || _gridInView.y >= _verticalDimension);
			_gridInView.y = _gridInView.y >= 0 ? _gridInView.y % _verticalDimension : _gridInView.y % _verticalDimension + _verticalDimension;

			// move the tile to new place
			x = _gridInView.x * _width;
			y = _gridInView.y * _height;
			
			if (xReload || yReload)
			{
				clean(url);
				load3();
			}
		}
		
		public function centerTo(xcP:Number, ycP:Number):void
		{
			_centerPixInScene.x = xcP;
			_centerPixInScene.y = ycP;
			var preCenterGrid:Point = _centerGridGridInScene;
			_centerGridGridInScene = pToG(_centerPixInScene);
			
			
			//cal Grid in view
			if(_canGetPreCenter == true)
			{
				_gridInView.x = _gridInView.x - (_centerGridGridInScene.x - preCenterGrid.x);
				_gridInView.y = _gridInView.y - (_centerGridGridInScene.y - preCenterGrid.y);
			}
			else
			{
				_canGetPreCenter = true;
			}
			
			var xReload:Boolean = (_gridInView.x < 0 || _gridInView.x >= _horizontalDimension);
			var yReload:Boolean = (_gridInView.y < 0 || _gridInView.y >= _verticalDimension);
			_gridInView.x = _gridInView.x >= 0 ? _gridInView.x % _horizontalDimension : _gridInView.x % _horizontalDimension + _horizontalDimension;
			_gridInView.y = _gridInView.y >= 0 ? _gridInView.y % _verticalDimension : _gridInView.y % _verticalDimension + _verticalDimension;
		
		
			//cal pix in view
			var pixInView:Point = groundLayer.gridPixInView(_gridInView);
			x = pixInView.x;
			y = pixInView.y;
			
			//cal grid in scene
			var preGridInScene:Point = _gridInScene.clone();
			_gridInScene.x = _centerGridGridInScene.x + (_gridInView.x - groundLayer.centerGridGridInView().x);
			_gridInScene.y = _centerGridGridInScene.y + (_gridInView.y - groundLayer.centerGridGridInView().y);
			
			if(_loaded == false)
			{	
				if(_gridInScene.x >= 0 && _gridInScene.y >= 0)
				{
					load();
					_loaded = true;
				}
			}
			else
			{
				if(xReload || yReload) load();//if(preGridInScene.x != _gridInScene.x || preGridInScene.y != _gridInScene.y)
			}
			drawTileInfo();
		}
		
		
		private function get groundLayer():GroundLayer
		{
			return GroundLayer(parent);
		}
			
		
		private function pToG(pix:Point):Point
		{
			var grid:Point = new Point;
			grid.x = uint(pix.x / _width);
			grid.y = uint(pix.y / _height);
			return grid;
		}
		
		
		private function getURL():String
		{
			return _mapURL + "/" + (_gridInScene.y) + "/" + (_gridInScene.x) + ".jpg";
		}
		
		
		
		private function load():void
		{
			if(_curURL)
			{
				clean(_curURL);
			}
			
			_curURL = getURL();
			ResourceCacher.instance.loadImage(_curURL,
			function handleResource(data:Object):void
			{
				bitmapData = BitmapData(data);
			});
		}
		
		private var _info:TextField = new TextField;
		private function drawTileInfo():void
		{
			groundLayer.addChild(_info);
			_info.x = x;
			_info.y = y;
			_info.width = 500;
			_info.height = 300;
			_info.textColor = 0XFFFFFF;
			_info.text = tileInfo();
		}
		private function tileInfo():String
		{
			var info:String = "";
			info = info + "_curCenterGridPixInScene" + _centerPixInScene.x + _centerPixInScene.y + "\n";
			info = info + "_curCenterGridGridInScene" + _centerGridGridInScene.x + _centerGridGridInScene.y + "\n";
			info = info + "_curGridGridInScene" + _gridInScene.x + _gridInScene.y + "\n";
			info = info + "_curGridGridInView" + _gridInView.x + _gridInView.y + "\n";
			info = info + "_curGridPixInView" + x + y + "\n";
			info = info + "url" + _curURL + "\n";
			return info;
		}
		
		
		private function getURL3():String
		{
			return _mapURL + "/" + (_yOrigin + _gridInView.y) + "/" + (_xOrigin + _gridInView.x) + ".jpg";
		}
		
		private function load3():void
		{
			var url:String = getURL3();
			ResourceCacher.instance.loadImage(url,
			function handleResource(data:Object):void
			{
				bitmapData = BitmapData(data);
			});
		}
		
		private function clean(url:String):void
		{
			ResourceCacher.instance.cancel(url);
			bitmapData = null;
		}
	}
}